﻿using B.S.AuthCenter.Api.Read.Application.Command.Users;
using B.S.AuthCenter.Api.Read.DTOs;
using B.S.AuthCenter.Domain;
using B.S.AuthCenter.ErrorCount;
using B.S.AuthCenter.InfrasIntructure.Interface;
using MediatR;
using System.Data;
using System;
using System.Net.NetworkInformation;
using System.Diagnostics.Metrics;

namespace B.S.AuthCenter.Api.Read.Application.Handler.Users
{
    public class GetUserHandler : IRequestHandler<GetUserCommand, ApiResult<List<UserDTO>>>
    {
        IBaseRepository<UserModel> UserRepo;
        IBaseRepository<UserRoleModel> userroleRepo;
        IBaseRepository<RoleModel> roleRepo;


        public GetUserHandler(IBaseRepository<UserModel> userRepo, IBaseRepository<UserRoleModel> userroleRepo, IBaseRepository<RoleModel> roleRepo)
        {
            this.UserRepo = userRepo;
            this.userroleRepo = userroleRepo;
            this.roleRepo = roleRepo;
        }

        public  Task<ApiResult<List<UserDTO>>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<UserDTO>> res = new ApiResult<List<UserDTO>>();

                var users = UserRepo.GetAll().OrderByDescending(x => x.Id).Where(x => x.IsDeleted == false);
                var roles = roleRepo.GetAll().Where(x => x.IsDeleted == false);
                var userRoles = userroleRepo.GetAll().Where(x => x.IsDeleted == false);

                var linq = (from u in users
                            join ur in userRoles on u.Id equals ur.Uid into uur
                            from ur in uur.DefaultIfEmpty()
                            join r in roles on ur.Rid equals r.Id into ru
                            from r in ru.DefaultIfEmpty()
                            select new UserDTO
                            {
                                Id = u.Id,
                                UserName = u.UserName,
                                Sex = u.Sex,
                                RegionName = u.RegionName,
                                ProvinceName=u.ProvinceName,
                                CityName = u.CityName,
                                AddressDetail = u.AddressDetail,
                                Creator = u.Creator,
                                CreatorDate = u.CreatorDate,
                                Updator = u.Updator,
                                UpdatorDate = u.UpdatorDate,
                                RoleName = r.RoleName,
                                Pwd=u.Pwd,
                                DepartmentId=u.DepartmentId,
                            }).ToList();
                var mer = new Dictionary<int, UserDTO>();
                foreach (var item in linq.ToList())
                {
                    if (!mer.TryGetValue(item.Id, out var me))
                    {
                        me = new UserDTO
                        {
                            Id = item.Id,
                            UserName = item.UserName,
                            Sex = item.Sex,
                            RegionName = item.RegionName,
                            ProvinceName = item.ProvinceName,
                            CityName = item.CityName,
                            AddressDetail = item.AddressDetail,
                            Creator = item.Creator,
                            CreatorDate = item.CreatorDate,
                            Updator = item.Updator,
                            UpdatorDate = item.UpdatorDate,
                            RoleName = item.RoleName,
                            Pwd = item.Pwd,
                            DepartmentId = item.DepartmentId,
                        };
                        mer[item.Id] = me;
                    }
                    else
                    {
                        me.RoleName += "," + item.RoleName;
                    }
                }
                res.Msg = "获取用户信息";
                res.Code = ApiEnum.Success;
                res.Datas = mer.Values.ToList();
                return  Task.FromResult(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
